// 
//  Copyright (C) 20012 Luca Piccioni
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Windows.Forms;

using Derm.OpenGL;

namespace Derm.Render
{
	/// <summary>
	/// An opaque handle that represents the platform dependent device context.
	/// </summary>
	public interface IDeviceContext
	{
		void Release();
	}

	class DeviceContextFactory
	{
		public static IDeviceContext Create(Control window)
		{
			switch (Environment.OSVersion.Platform) {
				case PlatformID.Win32Windows:
					return (new WindowsDeviceContext(window));
				case PlatformID.Unix: {
						XServerDeviceContext x11DeviceContext = new XServerDeviceContext();

						x11DeviceContext.Display = Glx.XOpenDisplay(IntPtr.Zero);
						x11DeviceContext.Screen = Glx.XDefaultScreen(x11DeviceContext.Display);
						x11DeviceContext.WindowHandle = window.Handle;

						return (x11DeviceContext);
					}
				default:
					throw new NotSupportedException(String.Format("platform {0} not supported", Environment.OSVersion));
			}
		}
	}

	/// <summary>
	/// Device context for MS Windows platform.
	/// </summary>
	class WindowsDeviceContext : IDeviceContext
	{
		/// <summary>
		///
		/// </summary>
		/// <param name="window">
		/// A <see cref="Control"/>
		/// </param>
		public WindowsDeviceContext(Control window)
		{
			if (window == null)
				throw new ArgumentNullException("window");

			WindowHandle = window.Handle;
			DeviceContext = Wgl.GdiGetDC(window.Handle);

			if (DeviceContext == IntPtr.Zero)
				throw new InvalidOperationException("unable to get any video device context");
		}

		public void Release()
		{
			Wgl.GdiReleaseDC(WindowHandle, DeviceContext);
		}

		/// <summary>
		/// The window handle.
		/// </summary>
		public readonly IntPtr WindowHandle;

		/// <summary>
		/// The device context of the control.
		/// </summary>
		public readonly IntPtr DeviceContext;
	}

	/// <summary>
	///Device context for Unix-based platforms.
	/// </summary>
	class XServerDeviceContext : IDeviceContext
	{
		/// <summary>
		/// Initializes the <see cref="Derm.Render.XServerDeviceContext"/> class.
		/// </summary>
		static XServerDeviceContext()
		{
			// Ensure to have X11 thread system initialized
			if (sX11ThreadsInitialized == false) {
				Glx.XInitThreads();
				sX11ThreadsInitialized = true;
			}
		}

		public XServerDeviceContext()
		{

		}

		public void Release()
		{
			if (Display != IntPtr.Zero) {
				Glx.XCloseDisplay(Display);
				Display = IntPtr.Zero;
			}
		}

		/// <summary>
		/// The opened display.
		/// </summary>
		public IntPtr Display;

		/// <summary>
		/// The screen.
		/// </summary>
		public int Screen;

		/// <summary>
		/// The window handle.
		/// </summary>
		public IntPtr WindowHandle;

		/// <summary>
		/// The visual configuration.
		/// </summary>
		public IntPtr VisualConfig;

		/// <summary>
		/// Flag indicating whether XServer thread system is initialized.
		/// </summary>
		private static bool sX11ThreadsInitialized;
	}
}

